package cn.fyl.org.service.impl;

import cn.fyl.basic.service.impl.BaseServiceImpl;
import cn.fyl.basic.util.PageList;
import cn.fyl.org.domain.Employee;
import cn.fyl.org.mapper.EmployeeMapper;
import cn.fyl.org.query.EmployeeQuery;
import cn.fyl.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee) {
        employeeMapper.save(employee);
        // 添加信息到中间表
        employeeMapper.saveEmployeeRole(employee.getId(),employee.getRole_id());
    }

    @Override
    public void remove(Long id) {
        // 删除中间表对应信息
        employeeMapper.removeEmployeeRoleByEmployeeId(id);
        employeeMapper.remove(id);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);

        //删除原来的数据
        employeeMapper.removeEmployeeRoleByEmployeeId(employee.getId());
        // 重新添加
        employeeMapper.saveEmployeeRole(employee.getId(),employee.getRole_id());
    }


    @Override
    public List<Employee> findByShopId(Long id) {
        return employeeMapper.findByShopId(id);
    }
}
